package com.clinbrain;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @author Xiahu
 * @create 2022/6/21 0021
 */
public class HttpTool {
    private final static String boundary = "*****";
    private final static String lineEnd = "\r\n";


    public static String doPostFormData(String url, InputStream inputStream) throws Exception {
        HttpURLConnection connection = null;
        OutputStream outputStream = null;
        FileInputStream fileInputStream = null;

        try {
            URL serverUrl = new URL(url);
            connection = (HttpURLConnection) serverUrl.openConnection();
            connection.setRequestMethod("POST");
            connection.setDoOutput(true);

            String boundary = "*****";
            String lineEnd = "\r\n";

            // 设置请求头
            connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);

            outputStream = connection.getOutputStream();
            // fileInputStream = new FileInputStream(file);

            // 文件表单字段名为 "paper_0"
            String fieldName = "paper_0";
            String file = "data.json";

            // 写入表单数据
            outputStream.write(("--" + boundary + lineEnd).getBytes());
            outputStream.write(("Content-Disposition: form-data; name=\"" + fieldName + "\"; filename=\"" + file + "\"" + lineEnd).getBytes());
            outputStream.write(("Content-Type: application/octet-stream" + lineEnd).getBytes());
            outputStream.write(lineEnd.getBytes());

            // 读取文件内容并写入请求体
            byte[] buffer = new byte[4096];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }

            // 请求体结束标志
            outputStream.write(lineEnd.getBytes());
            outputStream.write(("--" + boundary + "--" + lineEnd).getBytes());

            // 发送请求
            int responseCode = connection.getResponseCode();

            if (responseCode == HttpURLConnection.HTTP_OK) {
                InputStream responseStream = connection.getInputStream();
                // 使用 BufferedReader 读取响应数据
                BufferedReader reader = new BufferedReader(new InputStreamReader(responseStream));
                String line;
                StringBuilder response = new StringBuilder();
                while ((line = reader.readLine()) != null) {
                    response.append(line);
                }
                reader.close();
                return response.toString();
            }

            throw new RuntimeException("request url" + url + "error");
        } finally {
            // 关闭连接和流
            if (outputStream != null) {
                outputStream.close();
            }
        }
    }

    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new FileReader("D:\\code\\klbr_gitlab\\big-data-components\\presto-udf\\4paper00.json"));
        String json = br.readLine();
        ByteArrayInputStream inputStream = new ByteArrayInputStream(json.getBytes());
        String res = doPostFormData("http://10.10.9.83:8081/main", inputStream);
        System.out.println(res);
    }
}
